package vg.modules.backlight.components;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import vg.core.AUserInterface;
import vg.core.AUserInterfaceElement;
import vg.core.IGraphView;
import vg.core.event.AUIEvent;
import vg.core.event.UIEventChangeView;
import vg.core.exception.PluginException;
import vg.core.plugin.IPlugin;
import vg.core.plugin.PluginParameter;
import vg.core.storableGraph.StorableAttribute;
import vg.core.storableGraph.StorableEdge;
import vg.core.storableGraph.StorableSubGraph;
import vg.core.storableGraph.StorableVertex;
public class CriticalPathSearcher {
private JButton button;
private IGraphView view;
private JDialog dialog;
private JComboBox attributeCombobox = new JComboBox();
private JComboBox directionCombobox = new JComboBox();
private JCheckBox maxMinCheck = new JCheckBox();
private StorableVertex from = null;
private StorableVertex to = null;
// -------------------------------------------------------------------------
public CriticalPathSearcher() {
this.button = new JButton(new ImageIcon("./data/resources/textures/critical.png"));
this.button.setToolTipText("Find critical path beetwen two vertices");
this.dialog = new JDialog((JFrame) null, "Critical path", true);
JPanel contentPane = new JPanel(new GridBagLayout());
dialog.setContentPane(contentPane);
JButton okButon = new JButton("ok");
JButton cancelButon = new JButton("cancel");
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5,
10, 5, 5), 0, 0);
contentPane.add(new JLabel("Attribute"), gbc);
gbc = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 5, 5),
100, 0);
contentPane.add(attributeCombobox, gbc);
gbc = new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 10, 5, 5), 0, 0);
contentPane.add(new JLabel("Maximum"), gbc);
gbc = new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 10, 5, 5), 0, 0);
contentPane.add(maxMinCheck, gbc);
gbc = new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 10, 5, 5), 0, 0);
contentPane.add(new JLabel("Direction"), gbc);
gbc = new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 5, 5),
0, 0);
contentPane.add(directionCombobox, gbc);
JPanel okCancelPane = new JPanel();
okCancelPane.setLayout(new GridBagLayout());
gbc = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 5, 5),
0, 0);
okCancelPane.add(okButon, gbc);
gbc = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 5, 5),
0, 0);
okCancelPane.add(cancelButon, gbc);
gbc = new GridBagConstraints(0, 3, 2, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0,
0);
contentPane.add(okCancelPane, gbc);
dialog.setResizable(false);
dialog.pack();
okButon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
String attr = (String) attributeCombobox.getSelectedItem();
if (directionCombobox.getSelectedIndex() == 0)
CriticalPathSearcher.this.view.showCriticalPath(from, to, maxMinCheck.isSelected(), attr);
else
CriticalPathSearcher.this.view.showCriticalPath(to, from, maxMinCheck.isSelected(), attr);
}
});
cancelButon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
this.button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StorableSubGraph ssg = CriticalPathSearcher.this.view.getSelectionSubGraph();
if (ssg != null) {
List<StorableVertex> lsv = ssg.getVertices();
if (lsv != null && lsv.size() == 2) {
CriticalPathSearcher.this.from = lsv.get(0);
CriticalPathSearcher.this.to = lsv.get(1);
Object[] possibilities = { "from " + from.getId() + " to " + to.getId(),
"from " + to.getId() + " to " + from.getId() };
directionCombobox.removeAllItems();
directionCombobox.addItem(possibilities[0]);
directionCombobox.addItem(possibilities[1]);
attributeCombobox.removeAllItems();
HashSet<String> attrset = new HashSet<String>();
for (StorableEdge edge : CriticalPathSearcher.this.view.getStorableSubGraph().getEdges()) {
for (StorableAttribute attr : edge.getStorableAttributes()) {
if (attr.getType().equals(Double.class.toString()) || attr.getType().equals(Integer.class.toString()))
attrset.add(attr.getName());
}
}
for (String attr : attrset) {
attributeCombobox.addItem(attr);
}
dialog.setVisible(true);
}
}
}
});
this.button.setEnabled(false);
}
// -------------------------------------------------------------------------
public JComponent getView() {
return (this.button);
}
public void setEnabled(boolean enable) {
this.button.setEnabled(enable);
}
public void changeView(IGraphView igv) {
this.view = igv;
if (view == null) {
setEnabled(false);
}
}
}